package com.softeem.yuedong_server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.yuedong_server.dto.Result;
import com.softeem.yuedong_server.entity.Album;
import com.softeem.yuedong_server.entity.Music;
import com.softeem.yuedong_server.service.AlbumService;
import com.softeem.yuedong_server.service.MusicService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Tag(name = "歌曲管理")
@RequestMapping("/music")
@RestController
public class MusicController extends BaseController{

    @Resource
    private MusicService musicService;

    @Resource
    private AlbumService albumService;

    /*歌曲添加*/
    @Operation(summary = "歌曲添加",description = "添加歌曲信息，需要提供基本的参数，例如名称和歌手")
    @PostMapping("/")
    public Result addMusic(@RequestBody Music music)
    {
        return musicService.save(music) ? Result.ok().setMsg("添加成功").setData(music) : Result.fail().setMsg("添加失败");
    }

    /*歌曲删除*/
    @Operation(summary = "歌曲删除",description = "根据提供的歌曲id删除歌曲，id必须提供")
    @DeleteMapping("/{id}")
    public Result delMusic(@PathVariable Integer id)
    {
        return musicService.removeById(id) ? Result.ok().setMsg("删除成功") : Result.fail().setMsg("删除失败");
    }

    /*歌曲更新*/
    @Operation(summary = "歌曲更新",description = "修改歌曲信息，id必须提供")
    @PutMapping("/")
    public Result updateMusic(@RequestBody Music music)
    {
        return musicService.updateById(music) ? Result.ok().setMsg("更新成功").setData(music) : Result.fail().setMsg("更新失败");
    }

    /*歌曲详情*/
    @Operation(summary = "歌曲详情",description = "必须提供id，查看歌曲详情，并显示歌曲的专辑信息")
    @GetMapping("/{id}")
    public Result findMusic(@PathVariable Integer id)
    {
        return musicService.getById(id) != null ? Result.ok().setMsg("查询成功").setData(musicService.getById(id)) : Result.fail().setMsg("查询失败");
    }

    /*分页查询歌曲列表提供当前页码page和页面大小*/
    @Operation(summary = "歌曲列表",description = "查询所有的歌曲信息，提供页码和每页大小，分页显示数据")
    @Parameters({
            @Parameter(name = "page",description = "页码",required = true),
            @Parameter(name = "limit",description = "每页大小",required = true)
    })
    @GetMapping("/list")
    public Result findAllMusic(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer limit) {
        //分页查询歌曲信息
        List<Music> list = musicService.list(new Page<Music>(page, limit));
        //获取所有专辑id集合(Stream流+lambda表达式)
        List<Integer> albumIds = list.stream().map(Music::getAid).distinct().collect(Collectors.toList());
        //根据专辑id集合查询专辑信息
        List<Album> albums = albumService.listByIds(albumIds);
        //将专辑集合转换为Map集合，其中id为key，album为value
        Map<Integer, Album> map = albums.stream().collect(Collectors.toMap(Album::getId, album -> album));
        //重新遍历歌曲集合，将专辑信息封装到歌曲对象中
        list.forEach(music -> {
            music.setAlbum(map.get(music.getAid()));
        });
        //查询总条数
        Long count = musicService.count();
        return Result.ok().setMsg("查询成功").setData(list).setCount(count);
    }
}
